package alingrad.engine.src.core;

import java.io.IOException;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

import alingrad.engine.src.Resources;
import alingrad.engine.src.gameobjects.GameObjectManager;

/**
 * Main Game Loop
 * @author alingrad
 *
 */
public class MainLoop extends TreeNode{
	/**
	 * Child of the loop. The game object manager.
	 */
	private GameObjectManager manager;

	/**
	 * Whether the game is paused or not.
	 */
	private boolean isPaused;

	/**
	 * Loads game files.
	 * @throws IOException
	 */
	public static void load() throws IOException {
		try {
			//Sets Display settings
			Display.setTitle("Test Game!");
			Display.setFullscreen(false);
			Display.setVSyncEnabled(false);
			try {
				// get modes
				DisplayMode chosenMode = null;
				DisplayMode[] dm = Display.getAvailableDisplayModes();

				for (int i=0;i<dm.length;i++) {
			          if ((dm[i].getWidth() == RenderManager.WIDTH) && (dm[i].getHeight() == RenderManager.HEIGHT)) {
			               chosenMode = dm[i];
			               break;
			          }
			    }
				if (chosenMode != null)
					Display.setDisplayMode(chosenMode);
	    	} catch (Exception e) {
	      			e.printStackTrace();
	      			System.out.println("Unable to enter fullscreen, continuing in windowed mode");
	    	}

			//Defaults to 640 x 480
			Display.create();
			//Input Initialization
			Keyboard.create();
			Mouse.create();

			//Sets up openGL settings
			GL11.glMatrixMode(GL11.GL_PROJECTION);
			GL11.glLoadIdentity();

			//Sets view to be 2D
			GL11.glOrtho(0.0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight(), 0, -1.0, 1.0);

			//Enables Textures
			GL11.glEnable(GL11.GL_TEXTURE_2D);

			//Enables Transparency
			GL11.glEnable(GL11.GL_BLEND);
			GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

			GL11.glDisable(GL11.GL_DEPTH_TEST);
		    GL11.glMatrixMode(GL11.GL_MODELVIEW);
		    GL11.glLoadIdentity();
		    GL11.glViewport(0, 0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight());

		    //Sets up content loading
			new Resources();
		} catch (Exception ex) {
			ex.printStackTrace(System.err);
			System.out.println("Error occurred during creation of display!\nGame exiting...");
			cleanup();
		}

		GameObjectManager.load();
	}

	/**
	 * Constructs the main game loop.
	 */
	public MainLoop() {
		isPaused = false;
		manager = new GameObjectManager();
	}

	@Override
	public void initialize() {
		manager.initialize();
	}

	@Override
	public void update(long delta) {
		//Checks for Display Issues
		if (Display.isCloseRequested()) {
			GameThread.finish();
		}
		else if (Display.isActive() && !isPaused) {
			manager.update(delta);

		}
		else {
			try {
				GameThread.sleep(100);
			}catch (InterruptedException ex) {
			}
		}
	}

	/**
	 * Cleans up anything left behind.
	 */
	public static void cleanup() {
		Display.destroy();
		Keyboard.destroy();
		Mouse.destroy();
	}

}
